﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class InputFieldEx : MonoBehaviour {

    // 输入法管理相关
	AndroidJavaObject m_AndroidObjectActivity;
	AndroidJavaObject m_AndroidObjectContext;
    private InputFieldExManager m_InputFieldHandler;
    public int m_InputFieldExID;

    // 界面显示
    public Text m_InputField;

    // 相关的时间
    public delegate void DelegateCmdEnter(object sender, string InputCmd, string FullLine);
    public delegate void DelegateValueChange(object sender, string InputCmd, string FullLine);
    public event DelegateCmdEnter CmdEnterEvent;
    public event DelegateValueChange ValueChangeEvent;

    // 相关的显示内容
    private string m_strCmdLine;          // 输入的命令内容
    private string m_strPerCmdLine;       // 前一个内容，用来和当前输入比较判断是否发生变化
    private string m_strTip;              // 命令前缀

    private InputType m_InputType;         // 输入模式

    public int GetInputFieldExID(){
		return m_InputFieldExID;

	}

    // 这个函数是给InputFieldHandler 调用的，InputFieldHandler会遍历所有InputFieldEx实例并使用此函数分配ID
    public void SetInputFieldExID(int ID){
		m_InputFieldExID = ID;
	}

    public void SetInputType(InputType type)
    {
        m_InputType = type;
    }

    public void SetTip(string strTip)
    {
        m_strTip = strTip;
        m_InputField.text = GetCmdFullLineWithInputType();
    }

    private string GetCmdFullLineWithInputType()
    {
        string strrv = "";
        if (m_InputType == InputType.Normal)
        {
            strrv = m_strTip + m_strCmdLine;
        }
        else if (m_InputType == InputType.Password)
        {
            string strPasswordMask = "";
            for (int i = 0; i < m_strCmdLine.Length; i++)
                strPasswordMask += "*";
            strrv = m_strTip + strPasswordMask;
        }
        else if(m_InputType == InputType.Disable)
        {
            strrv = m_strTip;
        }

        return strrv;
    }


    void Start()
    {
        // 键盘的控制类
        m_InputFieldHandler = Component.FindObjectOfType<InputFieldExManager>();
        m_strPerCmdLine = "";
        m_strCmdLine = "";
        m_InputFieldHandler.SetCurrentInputFieldEx(100);
    }

    void Update()
    {
		if (m_InputFieldExID == m_InputFieldHandler.GetCurrentInputFieldExID() && InputType.Disable != m_InputType)
        {
            // 忽略对英文的选择
            if(Input.inputString.Length >1)
            {
              if((int)Input.inputString[0] < 132)
                {
                    return;
                }
            }

            // 这个是从输入缓存中获取文字
            foreach (char c in Input.inputString)
            {
				if (c == '\b') { // has backspace/delete been pressed?
					if (m_strCmdLine.Length != 0) {
                        m_strCmdLine = m_strCmdLine.Substring (0, m_strCmdLine.Length - 1);
					}
				} else if ((c == '\n') || (c == '\r')) { // enter/return
                    string strTmpFullCmd;
                    string strtmpCmdLine;

                    strTmpFullCmd = GetCmdFullLineWithInputType();
                    strtmpCmdLine = m_strCmdLine;

                    m_strTip = "";
                    m_strCmdLine = "";
                    CmdEnterEvent(this, strtmpCmdLine, strTmpFullCmd);
                }
                else {
                    m_strCmdLine += c;
				}
			}

            if(m_strPerCmdLine != m_strCmdLine)
            {
                m_strPerCmdLine = m_strCmdLine;
                string strfullline = GetCmdFullLineWithInputType();
                m_InputField.text = strfullline;
                ValueChangeEvent(this, m_strCmdLine, strfullline);
            }
        }	
	}

    public void OpenInputFieldExByOther()
    {
        EventSystem.current.SetSelectedGameObject(m_InputField.gameObject);
        OpenInputFieldEx();
    }

    // Use this for initialization
    public void OpenInputFieldEx()
    {
        // 根据不同平台使用不同的代码
        if (Application.platform == RuntimePlatform.Android)
        {
            // 向Handler注册当前使用键盘的是哪个实例
            m_InputFieldHandler.SetCurrentInputFieldEx(m_InputFieldExID);
            // 接下来应该是调用使用java写的键盘实例了
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            m_AndroidObjectActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            m_AndroidObjectContext = m_AndroidObjectActivity.Call<AndroidJavaObject>("getApplicationContext");
            var plugin = new AndroidJavaClass("openkeyboard.windforceworld.com.keyboardplugin.PluginClass");
            Debug.Log(plugin.CallStatic<string>("OpenKeyBoard", m_AndroidObjectContext));
        }
        else
        {
            m_InputFieldHandler.SetCurrentInputFieldEx(m_InputFieldExID);
            TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, false, "");
            TouchScreenKeyboard.hideInput = true;
        }
        //#if UNITY_ANDROID
        //#else
        //#endif
    }

}
